<?php

namespace app\shop\controller\info;

use app\shop\model\info\Info as InfoModel;
use app\shop\model\menu\Menu as MenuModel;
use app\shop\controller\Controller;

/**
 * 内容管理控制器
 */
class Info extends Controller
{
    /**
     * 列表
     */
    public function index()
    {
        // 获取全部列表
        $model = new InfoModel;
        $list = $model->getList($this->postData());
        // 栏目
        $menu = (new MenuModel)->getALLList();
        return $this->renderSuccess('', compact('list', 'menu'));
    }

    /**
     * 添加内容
     */
    public function add($menu_id)
    {
        // get请求
        if ($this->request->isGet()) {
            $menu = (new MenuModel)->getALLList($menu_id);
            $menuInfo = MenuModel::detail($menu_id);
            $model = [];
            if ($menuInfo['mould_id'] == 4) {
                $model = InfoModel::getSingleDetail($menu_id);
            }
            return $this->renderSuccess('', compact('menu', 'model'));
        }
        $model = new InfoModel;
        if ($model->add($this->postData())) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 内容编辑
     */
    public function edit($info_id)
    {
        $model = InfoModel::detail($info_id);
        if ($this->request->isGet()) {
            $menu = (new MenuModel)->getALLList($model['menu_id']);
            return $this->renderSuccess('', compact('model', 'menu'));
        }
        if (!$model) {
            $model = new InfoModel();
        }
        // 更新记录
        if ($model->edit($this->postData())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 修改内容状态
     */
    public function state($info_id, $status)
    {
        // 详情
        $model = InfoModel::detail($info_id);
        if (!$model->setStatus($status)) {
            return $this->renderError('操作失败');
        }
        return $this->renderSuccess('操作成功');
    }

    /**
     * 删除内容
     */
    public function delete($info_id)
    {
        // 详情
        $model = InfoModel::detail($info_id);
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

}
